//关系：夜间连接基站、家宽地址、通信、主副卡
//用group_fam_id属性存储家庭圈id
//构建索引，需要给多种label节点的group_fam_id都设置为索引
CREATE INDEX group_fam_for_user FOR (n:User) ON (n.group_fam_id);
CREATE INDEX group_fam_for_phone FOR (n:Phone) ON (n.group_fam_id);
CREATE INDEX group_fam_for_addr FOR (n:Addr) ON (n.group_fam_id);
CREATE INDEX group_fam_for_lac FOR (n:LAC) ON (n.group_fam_id);

//子图构建
CALL gds.graph.drop('famGraph');
MATCH (source:User|Phone)
OPTIONAL MATCH (source)-[r:has|called|night_connect|lives_in]-(target:Phone|Addr|Phone|LAC)
RETURN gds.graph.project(
  'famGraph',
  source,
  target,
  {
    relationshipProperties: r { .weight }
  },
  { undirectedRelationshipTypes: ['*'] }
);

//执行louvain计算并写入属性group_fam_id
CALL gds.louvain.write('famGraph', {
  writeProperty: 'group_fam_id',
  maxLevels:1,
  maxIterations:2,
  relationshipWeightProperty: 'weight'
})
YIELD communityCount, modularity, modularities;


//统计标签个数
MATCH (n)
WHERE n.group_fam_id IS NOT NULL
RETURN n.group_fam_id AS group_fam_id, count(n) AS nodeCount
ORDER BY nodeCount DESC

//查看某社群
WITH 377 AS comid MATCH (e)-[r]-(e2) WHERE e.group_fam_id = comid AND e2.group_fam_id = comid RETURN *

